package com.itheima.one_to_many;

import com.itheima.bean.Card;
import com.itheima.bean.Classes;
import com.itheima.bean.Student;
import com.itheima.one_to_one.CardMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        // 通过sqlSession工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 获取CardMapper接口的实现类对象
        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);

        // 执行映射配置文件中的sql语句，并接收结果
        List<Classes> list = mapper.selectAll();

        // 处理结果
        for (Classes classes : list) {
            System.out.println(classes.getId()+","+classes.getName());
            List<Student> stu = classes.getStudents();
            for (Student stus : stu) {
                System.out.println("\t"+stus);
            }

        }

        // 释放资源
        sqlSession.close();
        is.close();
    }
}
